
package SystemDataTypes;

import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Administrador
 */
public class FlightData {

    public double speed;    // velocidad
    public Vector3 position;   // position   
    public int flightId;    // el id del vuelo  
    public String source;   // de quien proviene el dato (RADAR, TRANSCEPTOR, etc)
  //  public boolean cancelled; // esta cancelado?
    public int wait;          // tiempo de espera
        
    public FlightData(double speed, Vector3 position, int flightId, String source, int wait) {
        this.speed = speed;
        this.position = position;
        this.flightId = flightId;
        this.source = source;
  //      this.cancelled = cancelled;
        this.wait = wait;
    }
       
    FlightData(JSONObject jSONObject) {
        
        try {

            if (jSONObject.has("speed"))
                this.speed = jSONObject.getDouble("speed");
            
            if (jSONObject.has("flightId"))
                this.flightId = jSONObject.getInt("flightId");
            
            if (jSONObject.has("source"))
                this.source = jSONObject.getString("source");

     //       if (jSONObject.has("cancelled"))
       //         this.cancelled = jSONObject.getBoolean("cancelled");

            if (jSONObject.has("wait"))
                this.wait = jSONObject.getInt("wait");
            
            if (jSONObject.has("position")){
                JSONArray array = jSONObject.getJSONArray("position");
                this.position = new Vector3();
                this.position.x = array.getDouble(0);
                this.position.y = array.getDouble(1);
                this.position.z = array.getDouble(2);
            }                                
                        
        } catch (JSONException ex) {
            Logger.getLogger(FlightData.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
    
  public FlightData(ResultSet rs) {
        try {
      //      this.cancelled = rs.getBoolean("Cancelled");
            this.flightId = rs.getInt("flightId");
            this.source = rs.getString("Source");
            this.speed = rs.getDouble("Speed");
            this.wait = rs.getInt("Wait");            
            String posStr = rs.getString("Position");
            JSONArray array = new JSONArray(posStr);
            this.position = new Vector3(array.getDouble(0),array.getDouble(1),array.getDouble(2));            
        } catch (Exception ex) {
            Logger.getLogger(FlightData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  
  @Override
  public String toString(){
        JSONObject container = new JSONObject();       
        JSONObject data = new JSONObject();              

        try {
            data.put("speed", this.speed);
            data.put("flightId", this.flightId);
            data.put("source", this.source);
        //    data.put("cancelled", this.cancelled);
            data.put("wait", this.wait);              
            JSONArray jposition = new JSONArray();        
            jposition.put(this.position.x);
            jposition.put(this.position.y);
            jposition.put(this.position.z);

            data.put("position", jposition);
            
            container.put("FlightData", data);
            
        } catch (JSONException ex) {
            Logger.getLogger(FlightData.class.getName()).log(Level.SEVERE, null, ex);
        }
        
  return container.toString();
  }
  
}
